package com.diligent.cloud.sfc.resources.impl;

import com.canny.commons.model.Codes;
import com.canny.commons.model.ObjectResult;
import com.canny.commons.model.Result;
import com.diligent.cloud.logistics.container.model.LcmBoxFill;
import com.diligent.cloud.sfc.model.enums.StopReason;
import com.diligent.cloud.sfc.model.params.MatListParams;
import com.diligent.cloud.sfc.model.params.WoOrderCompleteParams;
import com.diligent.cloud.sfc.resources.WorkshopSceneResource;
import com.diligent.cloud.sfc.service.*;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RestController;

import java.security.Principal;

/**
 * WorkshopSceneResourceImpl class
 *
 * @author xf107278
 * @date 3/28/2020
 */
@RestController
public class WorkshopSceneResourceImpl implements WorkshopSceneResource {


    private WorkshopSceneService workshopSceneService;
    private WorkshopSceneSprayingProcessService workshopSceneSprayingProcessService;
    private WorkShopPostProcessService workShopPostProcessService;
    private WorkshopScenePackingService workshopScenePackingService;
    private final WorkshopTransportService workshopTransportService;

    public WorkshopSceneResourceImpl(WorkshopSceneService workshopSceneService,
                                     WorkshopSceneSprayingProcessService workshopSceneSprayingProcessService,
                                     WorkShopPostProcessService workShopPostProcessService,
                                     WorkshopScenePackingService workshopScenePackingService, WorkshopTransportService workshopTransportService) {
        this.workshopSceneService = workshopSceneService;
        this.workshopSceneSprayingProcessService = workshopSceneSprayingProcessService;
        this.workShopPostProcessService = workShopPostProcessService;
        this.workshopScenePackingService = workshopScenePackingService;

        this.workshopTransportService = workshopTransportService;
    }

    @Override
    public ResponseEntity<Result> machStop(StopReason stopReason, String remark, String machCode,
                                           Principal principal) {
        return workshopSceneService.machStop(stopReason, remark, machCode, principal);
    }

    @Override
    public ResponseEntity<Result> getMachStopCode() {
        ObjectResult objectResult = new ObjectResult(StopReason.values());
        objectResult.modifyCode(Codes.success.getCode(), Codes.success.getMsg());
        return ResponseEntity.ok(objectResult);
    }


    @Override
    public ResponseEntity<ObjectResult> updateMat(String matCode, String matName, String machCode,  Principal principal) {
        return workshopSceneService.updateMat(matCode, matName,machCode, principal);
    }


    @Override
    public ResponseEntity<Result> modifyMdStatus(String matCode, Principal principal) {
        return workshopSceneService.modifyMdStatus(matCode, principal);
    }


    @Override
    public ResponseEntity<Result> modifyAaStatus(Long asId, Principal principal) {
        return workshopSceneService.modifyAsStatus(asId, principal);
    }

//    @Override
//    public ResponseEntity<Result> insertMfgMachEven(Principal principal) {
//        return workshopSceneService.insertMfgMachEven(principal);
//    }

    @Override
    public ResponseEntity<Result> updateMould(Long mdId, Long orderId, String accessToken, String previousModelId,
                                              Principal principal) {
        try {
            return workshopSceneService.updateMould(mdId, orderId, accessToken, previousModelId, principal);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public ResponseEntity<Result> updateAluminumStick(Long asId, Long mdId, Long orderId, Principal principal) {
        return workshopSceneService.updateAluminumStick(asId, mdId, orderId, principal);
    }

//    @Override
//    public ResponseEntity<Result> getEquipmentMaintenance(Principal principal) {
//        return workshopSceneService.getEquipmentMaintenance(principal);
//    }

    @Override
    public ResponseEntity<Result> getDevEnvironmentInfo(String devMac, Principal principal) {
        return workshopSceneService.getDevEnvironmentInfo(devMac, principal);
    }


//    @Override
//    public ResponseEntity<Result> addMatMd(Principal principal) {
//        return workshopSceneService.addMatMd(principal);
//    }
//
//    @Override
//    public ResponseEntity<Result> addMatAa(Principal principal) {
//        return workshopSceneService.addMatAa(principal);
//    }


    @Override
    public ResponseEntity<Result> getMatList(MatListParams matListParams) {
        Boolean md = matListParams.getMatCodeCv() == null ? false : !"null".equalsIgnoreCase(matListParams.getMatCodeCv()) ? true : false;
        Boolean as = matListParams.getMaterial() == null ? false : !"null".equalsIgnoreCase(matListParams.getMaterial()) ? true : false;


        switch (matListParams.getOrderType()) {
            case 1:
                if (md) {
                    return workshopSceneService.getWarmingStatusMdList(matListParams.getMachCode(),
                            matListParams.getMatCodeCv(), matListParams.getSystemId());
                } else if (as) {
                    return workshopSceneService.getWarmingStatusAaList(matListParams.getMachCode(),
                            matListParams.getMaterial(), matListParams.getSystemId());
                }
                break;
            case 2:
                return ResponseEntity.ok(workshopSceneSprayingProcessService.getColorList(matListParams.getMachCode()
                        , matListParams.getMatCode()));
            case 3:
                return ResponseEntity.ok(workShopPostProcessService.getRubberStripList(matListParams.getMachCode(),
                        matListParams.getMatCode()));

            case 4:
                return ResponseEntity.ok(workshopScenePackingService.getPackingList(matListParams.getMachCode(),
                        matListParams.getMatCode()));
        }
        return null;
    }


    @Override
    public ResponseEntity<Result> moldRelease(Long mdId, Principal principal) {
        return workshopSceneService.moldRelease(mdId, principal);
    }


    @Override
    public ResponseEntity<Result> inbox(WoOrderCompleteParams orderCompleteParams, Principal principal) {
        ObjectResult<LcmBoxFill> objectResult = (ObjectResult) workshopSceneService.inbox(orderCompleteParams, principal).getBody();
        if (objectResult.getCode() == Codes.fail.getCode()) {
            return ResponseEntity.ok(objectResult);
        }
        LcmBoxFill lcmBoxFill = objectResult.getData();

        String lotCode = lcmBoxFill.getLcmBoxFillDetail().getLotCode();
        Long lotId = lcmBoxFill.getLcmBoxFillDetail().getLotId();
        Long matId = lcmBoxFill.getLcmBoxFillDetail().getMatId();
        workshopTransportService.updateBoxMat(matId, lotId, lotCode, lcmBoxFill.getLcmBoxFillDetail().getId());
        return ResponseEntity.ok(objectResult);
    }

    @Override
    public ResponseEntity<Result> inboxDetail(Long orderId, String prodBatch, String machCode, Long systemId) {
        return workshopSceneService.inboxDetail(orderId, prodBatch, machCode, systemId);
    }

    @Override
    public ResponseEntity<Result> getDevInfo(Boolean login, Principal principal) {

        return workshopSceneService.getDevInfo(login, principal);
    }

    @Override
    public ResponseEntity<Result> modifyDev(Long machId, Principal principal) {

        return workshopSceneService.modifyDev(machId, principal);
    }

    @Override
    public ResponseEntity<Result> getDevInfo(String machName, Principal principal) {
        return workshopSceneService.getDevInfo(machName);
    }


}
